<?php


namespace App\Http\Controllers\app\product;

use App\Models\ProductClass;

class utils
{
    // 结果分页
    public static function Paging($document_list, $page, $size)
    {
        $start = ($page - 1) * $size;
        $article = array_slice($document_list, $start, $size);
        $datas_list = [];
        $count = count($document_list);
        $datas_list['total'] = $count;
        $datas_list['data'] = $article;
        return $datas_list;
    }

    // 根据部门得到所有能看的产品类型id——list
    public static function get_classify($branch_id, $pid = '')
    {
        $where[] = ['status', 1];
        $where[] = ['app_status', 1];
        if ($pid != '') {
            $where[] = ['pid', $pid];
        }
        $product_classify_list = ProductClass::where($where)->whereHas('branches', function ($query) use ($branch_id) {
            $query->where('branch_id', $branch_id);
        })->get();
        $product_classifys = [];
        $product_classifys[] = $pid;
        if ($product_classify_list) {
            foreach ($product_classify_list as $product_classify) {
                $product_classifys[] = $product_classify->id;
            }
        }
        return $product_classifys;
    }

    // 处理产品查询结果
    public static function deal_product_result($product_list, $tag = null)
    {
        $products = [];
        if ($product_list) {
            foreach ($product_list as $product) {
                $product_dic['name'] = $product->name;
                $product_dic['classify_id'] = $product->class_id;
                $product_dic['classify_name'] = $product['product_class']['category_name'];
                switch ($product->time_unit) {
                    case 1:
                        $product_dic['time_unit'] = '日';
                        break;
                    case 2:
                        $product_dic['time_unit'] = '月';
                        break;
                    case 3:
                        $product_dic['time_unit'] = '年';
                        break;
                    default:
                        $product_dic['time_unit'] = '未知';
                }
                $product_dic['rate'] = $product->rate;
                $product_dic['money'] = $product->money;
                $product_dic['detail'] = $product->detail;
                $product_dic['content'] = $product->content;
                $product_dic['ceiling'] = $product->ceiling;
                $product_dic['sale_time'] = $product->sale_time;
                $product_dic['commission'] = $product->commission;
                switch ($product->commission_type) {
                    case 1:
                        $product_dic['commission_type'] = '年化业绩';
                        break;
                    case 2:
                        $product_dic['commission_type'] = '合同金额';
                        break;
                    case 3:
                        $product_dic['commission_type'] = '其他';
                        break;
                    default:
                        $product_dic['commission_type'] = '未知';
                }
                switch ($product->pay_type) {
                    case 2:
                        $product_dic['pay_type'] = '自然月';
                        break;
                    case 4:
                        $product_dic['pay_type'] = '到期付';
                        break;
                    case 5:
                        $product_dic['pay_type'] = '不付款';
                        break;
                    default:
                        $product_dic['pay_type'] = '未知';
                }
                $product_dic['pay_long'] = $product->pay_long;
                switch ($product->pay_time_unit) {
                    case 1:
                        $product_dic['pay_time_unit'] = '日';
                        break;
                    case 2:
                        $product_dic['pay_time_unit'] = '月';
                        break;
                    case 3:
                        $product_dic['pay_time_unit'] = '年';
                        break;
                    default:
                        $product_dic['pay_type'] = '未知';
                }
                $product_dic['long_time'] = $product->long_time;
                $product_dic['results_rate'] = $product->results_rate;
                $product_dic['year_type'] = $product->year_type;
                $product_dic['remark'] = $product->remark;
                if (isset($product_dic['tag'])) {
                    $tag_list = explode(',', $product_dic['tag']);
                    if ($tag) {
                        if (in_array($tag, $tag_list)) {
                            $products[] = $product_dic;
                        }
                    } else {
                        $products[] = $product_dic;
                    }
                } else {
                    $products[] = $product_dic;
                }
            }
        }
        return $products;
    }
}
